package com.nervenets.template.controller.setting;

import com.nervenets.general.aspect.validate.Unique;
import com.nervenets.general.entity.ResponseResult;
import com.nervenets.general.jwt.aspect.JwtRole;
import com.nervenets.general.jwt.aspect.JwtSecurity;
import com.nervenets.general.web.BaseTreeEntityController;
import com.nervenets.general.web.params.DefaultTreeQueryParams;
import com.nervenets.template.controller.setting.params.DictFormParams;
import com.nervenets.template.controller.setting.params.DictPagingParams;
import com.nervenets.template.hibernate.domain.common.Dict;
import com.nervenets.template.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.HashMap;
import java.util.List;

/**
 * 2020/6/10 16:32 created by Joe
 **/
@Api(value = "字典接口")
@RestController
@RequestMapping("/api/dict")
@JwtRole(group = "system", groupName = "系统管理", function = "dict", functionName = "字典管理")
public class DictController extends BaseTreeEntityController<Dict, DictService, DictFormParams, DictPagingParams, DefaultTreeQueryParams<Dict>, Dict, Dict> {
    /**
     * 通过codes获取所有的下级字典
     *
     * @param codes 多个编码
     * @return 所有的下级字典
     */
    @ApiOperation(value = "通过codes获取所有的下级字典")
    @PostMapping("/codes")
    @JwtSecurity(required = false)
    public ResponseEntity<ResponseResult<HashMap<String, List<Dict>>>> codes(@Valid @NotEmpty @Unique @RequestBody List<String> codes) {
        HashMap<String, List<Dict>> map = new HashMap<>();
        for (String code : codes) {
            Dict dict = service.findByCode(code);
            if (null != dict) {
                map.put(code, service.findChildrenByParentId(dict.getId()));
            }
        }
        return successMessage(map);
    }
}
